<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <!-- 省市区级联选择器-->
      <el-form-item label="地区">
        <el-col :span="6">
          <el-form-item prop="province">
            <el-select v-model="queryParams.province" placeholder="请选择省" clea
                       rable style="width: 200px" >
              <el-option v-for="item in provinceList" :key="item.code" :label="item.name" :value="item.name" @click="handleProvinceChange(item.code)">

              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="city">
            <el-select v-model="queryParams.city" placeholder="请选择市" clearabl
                       e style="width: 200px" :disabled="!queryParams.province">
              <el-option v-for="item in cityList" :key="item.code" :label="item.name" :value="item.name" @click="handleCityChange(item.code)">

              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="area">
            <el-select v-model="queryParams.area" placeholder="请选择区/县" clear
                       able style="width: 200px" :disabled="!queryParams.city">
              <el-option v-for="item in areaList" :key="item.code" :label="item.name" :value="item.name" @click="handleAreaChange(item.code)">

              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="country">
            <el-select v-model="queryParams.country" placeholder="请选择乡镇/街道"
                       clearable style="width: 200px" :disabled="!queryParams.area">
              <el-option v-for="item in streetList" :key="item.code" :label="item.name" :value="item.name">

              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="服务点名称" prop="posName">
        <el-input
          v-model="queryParams.posName"
          placeholder="请输入服务点名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="序列号" prop="serialCode">
        <el-input
          v-model="queryParams.serialCode"
          placeholder="请输入序列号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          style="width: 200px"
        >
          <el-option label="停用" :value="0" />
          <el-option label="正常" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ytj:pos:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ytj:pos:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ytj:pos:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ytj:pos:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="posList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区/县" align="center" prop="area" />
      <el-table-column label="乡镇/街道" align="center" prop="country" />
      <el-table-column label="详细地址" align="center" prop="detailAddress" />
      <el-table-column label="服务点名称" align="center" prop="posName" />
      <el-table-column label="序列号" align="center" prop="serialCode" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '正常' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ytj:pos:edit']"
          >修改</el-button>
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ytj:pos:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改一体机服务站点对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="posRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="地区">
          <AreaSelect :model="areaModel" @update:model="updateAreaModel" />
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="form.detailAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="服务点名称" prop="posName">
          <el-input v-model="form.posName" placeholder="请输入服务点名称" />
        </el-form-item>
        <el-form-item label="序列号" prop="serialCode">
          <el-input v-model="form.serialCode" placeholder="请输入序列号" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed, getCurrentInstance, toRefs } from 'vue'
import { listPos, getPos, delPos, addPos, updatePos, exportPos } from "@/api/nongjt/pos"
// 1.引⼊
import { areaSelect } from "@/composables/AreaSelect"

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const posList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    province: undefined,
    city: undefined,
    area: undefined,
    country: undefined,
    posName: undefined,
    serialCode: undefined,
    status: undefined
  },
  rules: {
    province: [{ required: true, message: "省不能为空", trigger: "blur" }],
    city: [{ required: true, message: "市不能为空", trigger: "blur" }],
    area: [{ required: true, message: "区/县不能为空", trigger: "blur" }],
    country: [{ required: true, message: "乡镇/街道不能为空", trigger: "blur" }],
    posName: [{ required: true, message: "服务点名称不能为空", trigger: "blur" }],
    serialCode: [{ required: true, message: "序列号不能为空", trigger: "blur" }],
    status: [{ required: true, message: "状态不能为空", trigger: "change" }]
  }
});

const { queryParams, form, rules } = toRefs(data);

// 使⽤ areaSelect 组合式函数
const {
  provinceList,
  cityList,
  areaList,
  streetList,
  initProvinceList,
  provinceChange,
  cityChange,
  areaChange,
  resetAreaList
} = areaSelect()
/** 查询一体机服务站点列表 */
function getList() {
  loading.value = true;
  listPos(queryParams.value).then(response => {
    posList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    province: undefined,
    city: undefined,
    area: undefined,
    country: undefined,
    detailAddress: undefined,
    posName: undefined,
    serialCode: undefined,
    status: 1
  };
  proxy.resetForm("posRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加一体机服务站点";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value[0];
  getPos(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改一体机服务站点";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["posRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updatePos(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPos(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const posIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除一体机服务站点编号为"' + posIds + '"的数据项？').then(function() {
    return delPos(posIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.$modal.confirm("是否确认导出所有一体机服务站点数据？").then(() => {
    return exportPos(queryParams.value);
  }).then(response => {
    proxy.download(response);
  });
}
// 定义对应⽅法
// 省份选择改变时
function handleProvinceChange(value) {
  // 重置市、区、街道
  queryParams.value.city = null
  queryParams.value.area = null
  queryParams.value.country = null
  cityList.value = []
  areaList.value = []
  streetList.value = []
  if (value) {
    provinceChange(value)
  }
}
// 市选择改变时
function handleCityChange(value) {
  // 重置区、街道
  queryParams.value.area = null
  queryParams.value.country = null
  areaList.value = []
  streetList.value = []
  if (value) {
    cityChange(value)
  }
}
// 区/县选择改变时
function handleAreaChange(value) {
  // 重置街道
  queryParams.value.country = null
  streetList.value = []
  if (value) {
    areaChange(value)
  }
}

getList();
initProvinceList()
</script>